package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.*;
import entity.*;

/**
 * Servlet implementation class ManageAdminServlet
 */
@WebServlet("/ManageArticleServlet")
public class ManageArticleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ManageArticleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		//response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		String type = request.getParameter("type");
		String value = request.getParameter("value");
		int article_id = Integer.parseInt(request.getParameter("id"));
		Article article = new Article();
		article.setArticle_id(article_id);
		
		if(type.equals("0")) {//type=0，是顶置操作
			if(value.equals("1")) {//value=1，是管理员取消顶置操作
				ArticleDAO.getInstance().cancelTopArticle(article);
				response.sendRedirect("Article_List_ofAdminServlet");
			}else if(value.equals("0")){//value=0，是管理员进行顶置操作
				ArticleDAO.getInstance().topArticle(article);
				response.sendRedirect("Article_List_ofAdminServlet");
			}else if(value.equals("2")) {//value=2,是用户取消顶置操作
				ArticleDAO.getInstance().cancelTopArticle_user(article);
				response.sendRedirect("My_Home");
			}else {//value=3,用户进行顶置操作
				ArticleDAO.getInstance().topArticle_user(article);
				response.sendRedirect("My_Home");
			}
		}else if(type.equals("1")){//type=1,是删除操作
			ArticleDAO.getInstance().delete(article_id);
			response.sendRedirect("Article_List_ofAdminServlet");
		}else {//type=2,是用户自己删除操作
			ArticleDAO.getInstance().delete(article_id);
			response.sendRedirect("My_Home");
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

}
